 package sza.oracle.apps.ap.oie.entry.webui;
 
 import java.io.PrintStream;

import java.sql.CallableStatement;
import java.sql.Types;

import oracle.apps.ap.oie.entry.webui.EntryFlowPageCO;
 import oracle.apps.ap.oie.server.ExpenseReportHeadersVOImpl;
 import oracle.apps.ap.oie.server.ExpenseReportHeadersVORowImpl;
import oracle.apps.ap.oie.server.ReceiptBasedLinesVOImpl;
import oracle.apps.ap.oie.server.ReceiptBasedLinesVORowImpl;
import oracle.apps.ap.oie.server.WebExpensesAMImpl;
 import oracle.apps.ap.oie.webui.NavigationUtility;
 import oracle.apps.fnd.common.VersionInfo;
import oracle.apps.fnd.framework.OAApplicationModule;
import oracle.apps.fnd.framework.OAException;
import oracle.apps.fnd.framework.OAViewObject;
import oracle.apps.fnd.framework.webui.OAPageContext;
 import oracle.apps.fnd.framework.webui.beans.OAWebBean;

import oracle.jbo.Row;
import oracle.jbo.domain.Number;

import oracle.jbo.server.DBTransaction;

import sza.oracle.apps.ap.oie.utility.Arith;
import sza.oracle.apps.ap.oie.utility.SzaOIEUtil;
 
 public class SzaEntryFlowPageCO extends EntryFlowPageCO
 {
   public static final String RCS_ID = "$Header$";
   public static final boolean RCS_ID_RECORDED = VersionInfo.recordClassVersion("$Header$", "%packagename%");
   
   private String attribute1 = "";
   private String attribute2 = "";
   private String attribute10 = "";
   private String attribute11 = "";
   private String attribute12 = "";
   private String attribute13 = "";
   private String attribute14 = "";
   private String attribute15 = "";
   private String projectId = "";
   private String taskId = "";
   private String returnValue = "";
   private String retCode = "";
 
   public void processRequest(OAPageContext pageContext, OAWebBean webBean)
   {
     String LogMessage = "";
     String LogFunction = "";
     LogFunction = "SZA SzaEntryFlowPageCO.processRequest";
 
     LogMessage = "SZA SzaEntryFlowPageCO.processRequest - Start";
 
     SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
     super.processRequest(pageContext, webBean);
 
     LogMessage = "SZA SzaEntryFlowPageCO.processRequest - End";
 
     SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
   }
 
   public void processFormRequest(OAPageContext pageContext, OAWebBean webBean)
   {
     String LogMessage = "";
     String LogFunction = "";
     LogFunction = "SZA SzaEntryFlowPageCO.processFormRequest";
 
     LogMessage = "SZA SzaEntryFlowPageCO.processFormRequest - Start";
 
     SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
     SzaOIEUtil.printContextParameter(pageContext, webBean);
 
     super.processFormRequest(pageContext, webBean);
 
     String CurrentPage = NavigationUtility.getCurrentPage(pageContext);
     LogMessage = "SZA SzaButtonsCO.processFormRequest - CurrentPage:" + CurrentPage;
 
     SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
     String source = pageContext.getParameter("source");
     LogMessage = "SZA SzaButtonsCO.processFormRequest - source:" + source;
 
     SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
     String event = pageContext.getParameter("event");
     LogMessage = "SZA SzaButtonsCO.processFormRequest - event:" + event;
 
     SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
     if (("GeneralInformationPG".equalsIgnoreCase(CurrentPage)) && ("OIENavBar".equalsIgnoreCase(source)) && ("goto".equalsIgnoreCase(event)))
     {
      
       WebExpensesAMImpl webexpensesamimpl = (WebExpensesAMImpl)pageContext.getApplicationModule(webBean);
 
       if (webexpensesamimpl != null)
       {
         webexpensesamimpl.resetRenderAdvanceDataTxnVar(Boolean.valueOf(true), Boolean.valueOf(false));
 
         String ApWebRenderAdvancesData = (String)pageContext.getTransactionValue("ApWebRenderAdvancesData");
 
         LogMessage = "SZA WebExpensesAMImpl.resetRenderAdvanceDataTxnVar - ApWebRenderAdvancesData:" + ApWebRenderAdvancesData;
 
         SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
 
         ExpenseReportHeadersVORowImpl expensereportheadersvorowimpl = (ExpenseReportHeadersVORowImpl)webexpensesamimpl.getExpenseReportHeadersVO().first();
 
         expensereportheadersvorowimpl.setPrepayNum("");
         expensereportheadersvorowimpl.setAdvanceInvoiceToApply(null);
       }
     }
       if (("CashAndOtherLinesPG".equalsIgnoreCase(CurrentPage)) && ("OIENavBar".equalsIgnoreCase(source)) && ("goto".equalsIgnoreCase(event))) {
           WebExpensesAMImpl am = (WebExpensesAMImpl)pageContext.getApplicationModule(webBean);
           OAViewObject HeaderVo = (OAViewObject)am.findViewObject("ExpenseReportHeadersVO");
           String costCenter = (String)HeaderVo.getCurrentRow().getAttribute("Attribute12");
//           am.getDBTransaction().commit();
           String TemplateId = (String)pageContext.getTransactionValue("TemplateId");
          // Number intReportHeaderId = am.getReportHeaderId();
           String templateType = isTravleTemplate(TemplateId,am);
//           System.err.println("template type is:"+templateType+ "templateid is :"+TemplateId);
//           System.err.println("orgId is:"+pageContext.getOrgId());
//           System.err.println("pageContext.getParameter(Location) :"+pageContext.getParameter("SzaOieProjectLovRN"));
           ReceiptBasedLinesVOImpl LineVo = (ReceiptBasedLinesVOImpl)am.findViewObject("CashAndOtherLinesVO");
           if("BUDGET".equals(templateType) || "TRA".equals(templateType)||"EXP".equals(templateType)){
              
                   if (LineVo != null)
                   {
                     LineVo.first();
                   
                     while (LineVo.hasNext())
                     {
                      
                       ReceiptBasedLinesVORowImpl row = (ReceiptBasedLinesVORowImpl)LineVo.getCurrentRow();
                       
                      
                       
                       if(row.getStartExpenseDate() !=null){
                             String projectName = row.getProjectAccountingContext();
                             String taskName = row.getExpenseGroup();
                             String attribute10 = row.getAttribute10();
                             setDefaultExpType(TemplateId,pageContext.getOrgId()+"",projectName,taskName,costCenter,attribute10,am);       
                             if("0".equals(retCode)){
                             row.setAttribute("WebParameterId", attribute1);
                             
                             row.setAttribute("ItemDescription", attribute2);
                             
                             if( "BUDGET".equals(templateType)||"EXP".equals(templateType)){
                                 row.setAttribute("Attribute10", attribute11);
                             }
                             
                             row.setAttribute("Attribute11", attribute11);
                             
                             row.setAttribute("Attribute12", attribute12);
                             
                             row.setAttribute("Attribute13", attribute13);
                             
                             row.setAttribute("Attribute14", attribute14);
                             
                             row.setAttribute("Attribute15", attribute15);
                             }else {
                                 throw new OAException(returnValue,OAException.ERROR);
                             }
                       } else {
                           row.remove();
                       }
                       LineVo.next();
                     }
                     LineVo.reset();
                  }
                  
           }else {
               if(LineVo != null){
                   LineVo.first();
                   while (LineVo.hasNext()){
                       ReceiptBasedLinesVORowImpl row = (ReceiptBasedLinesVORowImpl)LineVo.getCurrentRow();
                      
                       if(row.getStartExpenseDate() == null) {
                           row.remove();
                       }
                       LineVo.next();
                   }
                   LineVo.first();
               }
           }
       }
 
     LogMessage = "SZA SzaEntryFlowPageCO.processFormRequest - End";
 
     SzaOIEUtil.cmlinLog(pageContext, LogFunction, LogMessage);
   }
   
     private String isTravleTemplate(String TemplateId, OAApplicationModule am) {
         String returnValue = "";
         String callStmt = 
             " begin SZA_OIE_BUDGET_UTIL.GET_REPORT_TEMPLATE_TYPE(p_expense_report_id => :1,\n" + 
             "                          o_return_value       => :2); end;";
         DBTransaction txn = am.getOADBTransaction();
         CallableStatement cs = null;
         Row row = null;
       
             try {
                 cs = txn.createCallableStatement(callStmt, 1);
                 cs.setString(1, TemplateId);
                 cs.setString(2, returnValue);
                 cs.registerOutParameter(2, Types.VARCHAR);
                 cs.execute();
                 returnValue = cs.getString(2);
                
                 cs.close();
             } catch (Exception e) {
                 e.printStackTrace();
                 try {
                     cs.close();
                 } catch (Exception ex) {

                 }
             }
             return returnValue;
     }
     
     private void setDefaultExpType(String TemplateId,String orgId, String projectName,String taskName,String costCenter,String attribute10,OAApplicationModule am) {
        
         String callStmt = 
             " begin SZA_OIE_BUDGET_UTIL.get_expense_info(errbuf => :1,\n" + 
         "                          retcode       => :2," +
         "                          p_org_id       => :3," +
         "                          p_report_type_id       => :4," +
         "                          p_project_name       => :5," +
         "                          p_task_name       => :6," +
         "                          o_project_id       => :7," +
         "                          o_task_id       => :8," +
         "                          o_attribute1       => :9," +
         "                          o_attribute2       => :10," +
         "                          o_attribute10       => :11," +
         "                          o_attribute11       => :12," +
         "                          o_attribute12       => :13," +
         "                          o_attribute13       => :14," +
         "                          o_attribute14       => :15," +
         "                          o_attribute15       => :16," +
         "                          p_cost_center       => :17," +
         "                          p_attribute10       => :18" +
             "                          ); end;";
         DBTransaction txn = am.getOADBTransaction();
         CallableStatement cs = null;
         Row row = null;
       
             try {
                 cs = txn.createCallableStatement(callStmt, 1);
                
                 cs.registerOutParameter(1, Types.VARCHAR);
                 cs.registerOutParameter(2, Types.VARCHAR);
                 cs.setString(3, orgId);
                 cs.setString(4, TemplateId);
                 cs.setString(5, projectName);
                 cs.setString(6, taskName);
                 cs.registerOutParameter(7, Types.VARCHAR);
                 cs.registerOutParameter(8, Types.VARCHAR);
                 cs.registerOutParameter(9, Types.VARCHAR);
                 cs.registerOutParameter(10, Types.VARCHAR);
                 cs.registerOutParameter(11, Types.VARCHAR);
                 cs.registerOutParameter(12, Types.VARCHAR);
                 cs.registerOutParameter(13, Types.VARCHAR);
                 cs.registerOutParameter(14, Types.VARCHAR);
                 cs.registerOutParameter(15, Types.VARCHAR);
                 cs.registerOutParameter(16, Types.VARCHAR);
                 cs.setString(17,costCenter);
                 cs.setString(18,attribute10);
                 cs.execute();
                 returnValue = cs.getString(1);
                 retCode = cs.getString(2);
                 projectId = cs.getString(7);
                 taskId = cs.getString(8);
                 attribute1 = cs.getString(9);
                 attribute2 = cs.getString(10);
                 attribute10 = cs.getString(11);
                 attribute11 = cs.getString(12);
                 attribute12 = cs.getString(13);
                 attribute13 = cs.getString(14);
                 attribute14 = cs.getString(15);
                 attribute15 = cs.getString(16);
                 cs.close();
             } catch (Exception e) {
                 e.printStackTrace();
                 try {
                     cs.close();
                 } catch (Exception ex) {

                 }
             }
     }
 }



